package com.findme.test.itext.pdf;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: PdfTemplate
 * @description:
 * @author: find me
 * @create: 2022-04-13 13:45
 * @Version 1.0
 */
public class PdfTemplate<T> {
    public Paragraph drawTitle(String title) throws DocumentException, IOException {
        Font titleFont = FontFactory.getTitleFont();
        Paragraph paragraph = drawTitle(title, titleFont);
        return paragraph;
    }

    public Paragraph drawTitle(String title, Font font) {
        Paragraph titleParagraph = new Paragraph(title, font);
        titleParagraph.setAlignment(PdfPCell.ALIGN_RIGHT);
        //行间距
        titleParagraph.setLeading(14f);
        //设置左缩进
        //paragraph1.setIndentationLeft(12);
        //设置右缩进
        titleParagraph.setIndentationRight(100);
        //设置首行缩进
        //paragraph1.setFirstLineIndent(24);
        //设置段落上空白
        //paragraph1.setSpacingBefore(5f);
        //设置段落下空白
        titleParagraph.setSpacingAfter(10f);
        return titleParagraph;
    }

    public PdfPTable drawPdfContext(Object obj) throws DocumentException, IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Font contextFont = FontFactory.getContextFont();
        PdfPTable table = drawPdfBody(obj, contextFont);
        return table;
    }

    public PdfPTable drawPdfReceiptHolder(Object obj) throws DocumentException, IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Font receiptHolderFont = FontFactory.getReceiptHolderFont();
        PdfPTable table = drawPdfBody(obj, receiptHolderFont);
        return table;
    }

    public PdfPTable drawPdfBody(Object obj, Font font) throws NoSuchMethodException, InvocationTargetException,
            IllegalAccessException {
        Class aClass = obj.getClass();

        TableLayout tableLayout = (TableLayout) aClass.getAnnotation(TableLayout.class);
        Field[] declaredFields = aClass.getDeclaredFields();
        // 对于有 CellLayout 的字段 按照index排序，并且
        LinkedList<Field> collect = Arrays.stream(declaredFields).filter(item -> item.getAnnotation(CellLayout.class) != null).sorted(Comparator.comparingInt(field -> field.getAnnotation(CellLayout.class).index())).collect(Collectors.toCollection(LinkedList::new));
        // 设置样式
        PdfPTable contextTable = new PdfPTable(tableLayout.cols());
        for (Field field : collect) {
            CellLayout cellLayout = field.getAnnotation(CellLayout.class);

            CellFactory cellFactory = new CellFactory(font, cellLayout.horizontalAlignment(), cellLayout.verticalAlignment(), cellLayout.colspan(), cellLayout.rowspan(), cellLayout.borderSide(), cellLayout.minimumHeight());
            String name = field.getName();
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
            Method m = aClass.getMethod("get" + name);


            PdfPCell cell = cellFactory.generatePdfCell(cellLayout.title() + ((m.invoke(obj) != null) ? m.invoke(obj) :
                    ""));
            cell.setFollowingIndent(tableLayout.fontSize() * cellLayout.followingIndentRatio());
            contextTable.addCell(cell);
        }
        return contextTable;
    }


    public PdfPTable drawPdfExcel(List<T> objs, Class aClass, Font font) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        TableLayout tableLayout = (TableLayout) aClass.getAnnotation(TableLayout.class);

        PdfPTable excelTable = new PdfPTable(tableLayout.cols());
        Field[] declaredFields = aClass.getDeclaredFields();


        LinkedList<Field> collect = Arrays.stream(declaredFields).filter(item -> item.getAnnotation(HeadLayout.class) != null).sorted(Comparator.comparingInt(field -> field.getAnnotation(HeadLayout.class).index())).collect(Collectors.toCollection(LinkedList::new));
        for (Field field : collect) {
            HeadLayout headLayout = field.getAnnotation(HeadLayout.class);
            CellFactory cellFactory = new CellFactory(font, headLayout.horizontalAlignment(), headLayout.verticalAlignment(), headLayout.colspan(), headLayout.rowspan(), headLayout.borderSide(), headLayout.minimumHeight());
            PdfPCell cell = cellFactory.generatePdfCell(headLayout.headTitle());
            excelTable.addCell(cell);
        }


        for (T obj : objs) {
            Class<?> obj1 = obj.getClass();
//            Class obj1 = (Class) obj;
            Field[] qq = aClass.getDeclaredFields();
            // 对于有 CellLayout 的字段 按照index排序，并且
            LinkedList<Field> collect1 =
                    Arrays.stream(qq).filter(item -> item.getAnnotation(CellLayout.class) != null).sorted(Comparator.comparingInt(field -> field.getAnnotation(CellLayout.class).index())).collect(Collectors.toCollection(LinkedList::new));
            // 设置样式
            for (Field field : collect1) {
                CellLayout cellLayout = field.getAnnotation(CellLayout.class);
                CellFactory cellFactory = new CellFactory(font, cellLayout.horizontalAlignment(), cellLayout.verticalAlignment(), cellLayout.colspan(), cellLayout.rowspan(), cellLayout.borderSide(), cellLayout.minimumHeight());
                String name = field.getName();
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                Method m = obj1.getMethod("get" + name);
                PdfPCell cell = cellFactory.generatePdfCell(cellLayout.title() + ((m.invoke(obj) != null) ? m.invoke(obj) :
                        ""));
                cell.setFollowingIndent(tableLayout.fontSize() * cellLayout.followingIndentRatio());
                excelTable.addCell(cell);
            }
        }
        return excelTable;
    }


}
